package ciicos.service.impl;

import ciicos.entity.*;
import ciicos.mapper.LineDeviceMapper;
import ciicos.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import ciicos.util.RandomUtil;

import java.math.BigDecimal;

import cn.hutool.core.util.ObjectUtil;
import ciicos.permission.model.authorize.AuthorizeConditionModel;

import ciicos.util.GeneraterSwapUtil;
import ciicos.model.visiual.superQuery.ConditionJsonModel;
import ciicos.model.visiual.superQuery.SuperQueryJsonModel;
import ciicos.model.visiual.superQuery.SuperQueryConditionModel;


import ciicos.permission.service.AuthorizeService;


import java.lang.reflect.Field;

import com.baomidou.mybatisplus.annotation.TableField;

import java.util.stream.Collectors;

import ciicos.model.linedevice.*;


import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import java.text.SimpleDateFormat;

import ciicos.util.*;

import java.util.*;

import ciicos.base.model.filter.RuleInfo;
import ciicos.permission.util.GenUtil;

/**
 * 终端设备管理
 * 版本： V3.2.0
 * 版权：
 * 作者： 企业研发中心
 * 日期： 2025-10-06
 */
@Service
public class LineDeviceServiceImpl extends ServiceImpl<LineDeviceMapper, LineDeviceEntity> implements LineDeviceService {


    @Autowired
    private GeneraterSwapUtil generaterSwapUtil;

    @Autowired
    private UserProvider userProvider;

    @Autowired
    private AuthorizeService authorizeService;

    private final static Class eClass = LineDeviceEntity.class;

    @Override
    public List<LineDeviceEntity> getList(LineDevicePagination pagination) {
        String dataType = pagination.getDataType();

        QueryWrapper<LineDeviceEntity> queryWrapper = new QueryWrapper<>();
        AutoCodeUtil.highQuery(pagination.getSuperQueryJson(),eClass,queryWrapper);
        if (ObjectUtil.isNotEmpty(pagination.getCode())) {
            queryWrapper.lambda().like(LineDeviceEntity::getCode, pagination.getCode());
        }

        if (ObjectUtil.isNotEmpty(pagination.getName())) {
            queryWrapper.lambda().like(LineDeviceEntity::getName, pagination.getName());
        }

        //排序+分页
        return AutoCodeUtil.sortPageList(this,queryWrapper, eClass,
                pagination.getSidx(), pagination.getSort(),
                LineDeviceEntity::getId, true,pagination,StringUtil.equals("0",dataType));
    }

    @Override
    public void create(LineDeviceEntity entity) {
        this.save(entity);
    }

    @Override
    public boolean update(String id, LineDeviceEntity entity) {
        return this.updateById(entity);
    }

    @Override
    public void delete(LineDeviceEntity entity) {
        if (entity != null) {
            this.removeById(entity.getId());
        }
    }
    //子表方法

    //列表子表数据方法


    //验证表单唯一字段
    @Override
    public String checkForm(LineDeviceForm form, int i) {
        int total = 0;
        boolean isUp = StringUtil.isNotEmpty(form.getId()) && !form.getId().equals("0");
        String id = "";
        String countRecover = "";
        if (isUp) {
            id = form.getId();
        }
        if (ObjectUtil.isNotEmpty(form.getCode())) {
            QueryWrapper<LineDeviceEntity> codeWrapper = new QueryWrapper<>();
            codeWrapper.lambda().eq(LineDeviceEntity::getCode, form.getCode());
            if (isUp) {
                codeWrapper.lambda().ne(LineDeviceEntity::getId, id);
            }
            if ((int) this.count(codeWrapper) > 0) {
                total++;
                countRecover = "编码";
            }
        }

        return countRecover;
    }


}
